package com.tingyuge.base.jdk.io.aio;

import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;

/**
 * @Created by xiaodao
 */
public class AcceptHandler implements CompletionHandler<AsynchronousSocketChannel,AioServerHandler> {
    @Override
    public void completed(AsynchronousSocketChannel channel, AioServerHandler aioServerHandler) {
        AioServer.clientCount++;
        System.out.println("客户端的连接数: "+ AioServer.clientCount);
        //继续接受其他客户端的请求
        aioServerHandler.channel.accept(aioServerHandler,this);
        //创建新的Buffer
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        //异步读  第三个参数为接收消息回调的业务Handler
        channel.read(buffer, buffer, new ReadHandler(channel));

    }


    @Override
    public void failed(Throwable exc, AioServerHandler aioServerHandler) {
    exc.printStackTrace();
        System.err.println("服务端监听简历连接失败");
        aioServerHandler.latch.countDown();

    }
}
